/*
 * Copyright 2007-2008 Brian Cavalier
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  org.bc.iterate.view;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * IntegerRangeView is a {@link org.bc.iterate.view.View}  that iterates over a range
 * of integers.  There is no {@code Collection}  backing an IntegerRangeView.
 * 
 * @author Brian Cavalier
 * @see Iterator
 * @see  Iterable
 */
public class IntegerRangeView extends AbstractView<Integer> {
	protected int _start;
	protected int _end;

    /**
     * Creates an {@link IntegerRangeView} whose first value returned by {@link IntegerRangeView#next()} will
     * be {@code 0} and last value will be {@code Intebger.MAX_VALUE - 1}.  
     */

    public IntegerRangeView() {
		this(0, Integer.MAX_VALUE);
	}

    /**
     * Creates an {@link IntegerRangeView} whose first value returned by {@link IntegerRangeView#next()} will
     * be {@code start} and last value will be {@code end -1 }.
     */
    public IntegerRangeView(int start, int end) {
		_start = start;
		_end = end;
	}
	
    /**
     * Creates an {@link IntegerRangeView} whose first value returned by {@link IntegerRangeView#next()} will
     * be {@code start} and last value will be {@code Intebger.MAX_VALUE - 1}.  
     */
	public IntegerRangeView from(int start) {
		_start = start;
		return this;
	}

    /**
     * Creates an {@link IntegerRangeView} whose first value returned by {@link IntegerRangeView#next()} will
     * be {@code 0} and last value will be {@code end - 1}.  
     */
	public IntegerRangeView to(int end) {
		_end = end;
		return this;
	}

    /**
     * @return true iff the next value returned by {@code next()} would be strictly less than the {@code end} value
     * of this {@link org.bc.iterate.view.IntegerRangeView}, false otherwise.
     */
    public boolean hasNext() {
		return (_start < _end);
	}

    /**
     * @return the next largest {@code Integer} in the range.
     * @throw NoSuchElementException if the value {@code end - 1} has already been returned in a previous call
     * to {@code next()}  
     */
    public Integer next() {
        if(_start >= _end) {
            throw new NoSuchElementException();
        }
        return _start++;
	}
}